"use strict";
module.exports = {
    async readerData(instance, readerTemplateObject) {
        let OperateStatus;
        let FinalReaderValue;
        let readerPromise = new Promise((resolve, reject) => {
            let readerPoint = new instance.__InstanceRootControl.PluginPoint.mongodb.MongoClient(
                instance.__libs.Tools.__SpecialTools.getUrl(instance.__InstanceRootControl.confirmFinalData),
                {useUnifiedTopology: true}
            );
            readerPoint.connect((error, result) => {
                if (error === null || error === undefined) {
                    result
                        .db(readerTemplateObject.DataLibrary)
                        .collection(readerTemplateObject.CollectName)
                        .find(readerTemplateObject.SearchData)
                        .limit(readerTemplateObject.DataLimit)
                        .sort(readerTemplateObject.DataSort)
                        .skip(readerTemplateObject.DataSkip)
                        .toArray((err, res) => {
                            if (err) {
                                reject(err);
                            } else {
                                resolve(res);
                            }
                            readerPoint.close(true);
                        });
                } else {
                    reject(error);
                    readerPoint.close(true);
                }
            });
        });
        await readerPromise.then((val) => {
            FinalReaderValue = val;
            OperateStatus = true;
        }).catch((val) => {
            FinalReaderValue = val;
            OperateStatus = false;
        });
        if (OperateStatus) {
            return FinalReaderValue;
        } else {
            throw new Error(FinalReaderValue);
        }
    },
    async insertData(instance, insertTemplateObject) {
        let OperateStatus;
        let insertFinalValue;
        let insertPromise = new Promise((resolve, reject) => {
            let insertPoint = new instance.__InstanceRootControl.PluginPoint.mongodb.MongoClient(
                instance.__libs.Tools.__SpecialTools.getUrl(instance.__InstanceRootControl.confirmFinalData),
                {useUnifiedTopology: true}
            );
            insertPoint.connect((error, result) => {
                if (error === null || error === undefined) {
                    switch (instance.__ToolKit.DataTypeDetect(insertTemplateObject.insertData)) {
                        case "arr":
                            result
                                .db(insertTemplateObject.DataLibrary)
                                .collection(insertTemplateObject.CollectName)
                                .insertMany(insertTemplateObject.insertData, (err, res) => {
                                    if (err) {
                                        reject(`插入数据失败，错误信息：${err}`);
                                    } else {
                                        resolve(`插入数据成功，插入的数据量为：${res.insertedCount} 条。`);
                                    }
                                    insertPoint.close(true);
                                });
                            break;
                        case "obj":
                            result
                                .db(insertTemplateObject.DataLibrary)
                                .collection(insertTemplateObject.CollectName)
                                .insertOne(insertTemplateObject.insertData, (err, res) => {
                                    if (err) {
                                        reject(`插入数据失败，错误信息：${err}`);
                                    } else {
                                        resolve(`插入1条数据成功。`);
                                    }
                                    insertPoint.close(true);
                                });
                            break;
                    }
                } else {
                    reject(error);
                    insertPoint.close(true);
                }
            });
        });
        await insertPromise.then((val) => {
            OperateStatus = true;
            insertFinalValue = val;
        }).catch((val) => {
            OperateStatus = false;
            insertFinalValue = val;
        });
        if (OperateStatus) {
            return insertFinalValue;
        } else {
            throw new Error(insertFinalValue);
        }
    },
    async updateData(instance, updateTemplateObject) {
        let OperateStatus;
        let updateFinalValue;
        let updatePromise = new Promise((resolve, reject) => {
            let updatePoint = new instance.__InstanceRootControl.PluginPoint.mongodb.MongoClient(
                instance.__libs.Tools.__SpecialTools.getUrl(instance.__InstanceRootControl.confirmFinalData),
                {useUnifiedTopology: true}
            );
            updatePoint.connect((error, result) => {
                if (error === null || error === undefined) {
                    if (updateTemplateObject.multiDataUpdate) {
                        result
                            .db(updateTemplateObject.DataLibrary)
                            .collection(updateTemplateObject.CollectName)
                            .updateMany(
                                updateTemplateObject.updateData.where,
                                updateTemplateObject.updateData.updateString,
                                (err, res) => {
                                    if (err) {
                                        reject(`数据更新失败！错误信息：${err}`);
                                    } else {
                                        resolve(`成功更新${res.modifiedCount}条数据。`);
                                    }
                                    updatePoint.close(true);
                                });
                    } else {
                        result
                            .db(updateTemplateObject.DataLibrary)
                            .collection(updateTemplateObject.CollectName)
                            .updateOne(
                                updateTemplateObject.updateData.where,
                                updateTemplateObject.updateData.updateString,
                                (err, res) => {
                                    if (err) {
                                        reject(`数据更新失败！错误信息：${err}`);
                                    } else {
                                        resolve(`成功更新${res.modifiedCount}条数据。`);
                                    }
                                    updatePoint.close(true);
                                });
                    }
                } else {
                    reject(error);
                    updatePoint.close(true);
                }
            });
        });
        await updatePromise.then((val) => {
            OperateStatus = true;
            updateFinalValue = val;
        }).catch((val) => {
            OperateStatus = false;
            updateFinalValue = val;
        });
        if (OperateStatus) {
            return updateFinalValue;
        } else {
            throw new Error(updateFinalValue);
        }
    },
    async removeData(instance, removeTemplateObject) {
        let OperateStatus;
        let removeFinalValue;
        let removePromise = new Promise((resolve, reject) => {
            let removePoint = new instance.__InstanceRootControl.PluginPoint.mongodb.MongoClient(
                instance.__libs.Tools.__SpecialTools.getUrl(instance.__InstanceRootControl.confirmFinalData),
                {useUnifiedTopology: true}
            );
            removePoint.connect((error, result) => {
                if (error === null || error === undefined) {
                    if (removeTemplateObject.multiDataRemove) {
                        result
                            .db(removeTemplateObject.DataLibrary)
                            .collection(removeTemplateObject.CollectName)
                            .deleteMany(removeTemplateObject.removeData, (err, res) => {
                                if (err) {
                                    reject(`数据删除失败，错误信息：${err}`);
                                } else {
                                    resolve(`成功删除${res.deletedCount}条数据！`);
                                }
                                removePoint.close(true);
                            })
                    } else {
                        result
                            .db(removeTemplateObject.DataLibrary)
                            .collection(removeTemplateObject.CollectName)
                            .deleteOne(removeTemplateObject.removeData, (err, res) => {
                                if (err) {
                                    reject(`数据删除失败，错误信息：${err}`);
                                } else {
                                    resolve(`成功删除${res.deletedCount}条数据！`);
                                }
                                removePoint.close(true);
                            });
                    }
                } else {
                    reject(error);
                    removePoint.close(true);
                }
            });
        });
        await removePromise.then((val) => {
            OperateStatus = true;
            removeFinalValue = val;
        }).catch((val) => {
            OperateStatus = false;
            removeFinalValue = val;
        });
        if (OperateStatus) {
            return removeFinalValue;
        } else {
            throw new Error(removeFinalValue);
        }
    }
}